package com.demo.student;

import com.demo.bookshelf.BookShelfInterceptor;
import com.demo.bookshelf.BookShelfService;
import com.demo.bookshelf.BookShelfValidator;
import com.demo.common.model.Bookshelf;
import com.demo.common.model.Student;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;

@Before(StudentInterceptor.class)
public class StudentController extends Controller {

    @Inject
    StudentService studentService;

    public void index() {
        Kv cond=Kv.create();
        if(StrKit.notBlank(getPara("studentid"))){
            cond.set("studentid",getPara("studentid"));
        }
        if(StrKit.notBlank(getPara("studentname"))){
            cond.set("studentname",getPara("studentname"));
        }
        setAttr("studentPage", studentService.paginate(getParaToInt(0, 1), 10,Kv.by("cond",cond)));
        render("student.html");
    }

    public void add() {
    }

    /**
     * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
     * 并要对数据进正确性进行验证，在此仅为了偷懒
     */
    @Before(StudentValidator.class)
    public void save() {
        getBean(Student.class).save();
        redirect("/student");
    }

    public void edit() {
        setAttr("student", studentService.findById(getParaToInt()));
    }

    /**
     * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
     * 并要对数据进正确性进行验证，在此仅为了偷懒
     */
    @Before(StudentValidator.class)
    public void update() {
        getBean(Student.class).update();
        redirect("/student");
    }

    public void delete() {
        studentService.deleteById(getParaToInt());
        redirect("/student");
    }
}
